package com.tzxs.pay.main.login;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Toast;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivityLoginBinding;
import com.tzxs.pay.main.ContentActivity;
import com.tzxs.pay.main.baiduvoice.BaiduSpeechClient;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.login.listener.LoginListener;
import com.tzxs.pay.main.model.MerchantInfoGetter;
import com.tzxs.pay.util.ISharedPreference;
import com.tzxs.pay.util.StatusBarColorSetter;


//登录页面
public class LoginActivity extends BaseActivity<ActivityLoginBinding> {

    private static final String TAG = LoginActivity.class.getSimpleName();
    private LoginViewModel model;
    private ProgressDialog dialog;


    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }


    @SuppressLint("CheckResult")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, Color.parseColor("#71A0FB"));  //设置登录界面状态栏颜色
        //初始化viewModel  绑定lifecycle
        model = ViewModelProviders.of(this).get(LoginViewModel.class);
        dataBinding.setViewModel(model);

        //登录
        //在Activity中注册两个LiveData,
        model.snackbarEvent.observe(this, s -> Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show());
        dataBinding.loginAccount.setText(ISharedPreference.getInstance(getApplication()).getPhone());
        dataBinding.loginAccount.setSelection(dataBinding.loginAccount.getText().length());
        model.dialogEvent.observe(this, s -> {
            if (s == null) {
                if (dialog != null) {
                    dialog.dismiss();
                }
            } else {
                //我的dialog需要一个标题,所以我需要一个接口方法来获取标题
                dialog = ProgressDialog.show(LoginActivity.this, s, "请稍等……", false, false);

            }
        });
        BaiduSpeechClient.getInstance();

        //事件监听
        dataBinding.setListener(new LoginListener() {
            @Override
            public void login() {
                //登录
                String phone = dataBinding.loginAccount.getText().toString().trim();
                String password = dataBinding.loginPassword.getText().toString().trim();

                if (phone.length() != 11) {
                    model.snackbarEvent.setValue("请输入正确手机号");
                    return;
                }
                if (TextUtils.isEmpty(password) || TextUtils.isEmpty(phone)) {
                    model.snackbarEvent.setValue("手机号或密码不能为空");
                    return;
                }

                //登录成功
                model.login(dataBinding.loginAccount.getText().toString(), dataBinding.loginPassword.getText().toString(), () -> {
                    Intent intent = new Intent(LoginActivity.this, ContentActivity.class);
                    startActivity(intent);
                    finish();//登录成功关闭这个activity
                });
            }

            @Override
            public void isShowPassword() {
                if (model.isShowPassword.get()) {
                    //隐藏密码
                    model.isShowPassword.set(false);
                    dataBinding.loginPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    //显示密码
                    model.isShowPassword.set(true);
                    dataBinding.loginPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                dataBinding.loginPassword.setSelection(dataBinding.loginPassword.getText().length());
            }
        });
    }


}
